clear all
**set working directory**
cd "..."
version 15
set maxvar 30000


** import  Raw_Data on STATA using the "ztree2stata" package as provided by Kan Takeuchi, Graduate School of Economics, Hitotsubashi University, Japan.


ztree2stata subjects using 191125_1015.xls 
replace treatment = 0
gen Session = 1
keep if Period > 0
drop TimeOKStage0OK TimeStage1OpenFirstOK TimeStage2MoveOK TimeUEStage3OK TimeStage3OK TimeUEStage4OK TimeStage4OK TimeOKFinalStageOK finalchoice tables
drop Group Profit TotalProfit Participate roundprofits paid_period1 paid_period2 paid_period3 paid_period4 paid_period5 payoff1 payoff2
save "S1T0.dta", replace
clear all

ztree2stata subjects using 191125_1308.xls
gen Session = 1
keep if Period > 0
drop TimeOKStage0OK TimeStage1OpenFirstOK TimeStage2MoveOK TimeUEStage3OK TimeStage3OK TimeUEStage4OK TimeStage4OK TimeOKFinalStageOK finalchoice tables
drop Group Profit TotalProfit Participate roundprofits paid_period1 paid_period2 paid_period3 paid_period4 paid_period5 payoff1 payoff2
save "S1T1.dta", replace
clear all

ztree2stata subjects using 191129_0948.xls
replace treatment = 0
gen Session = 2
keep if Period > 0
drop TimeOKStage0OK TimeStage1OpenFirstOK TimeStage2MoveOK TimeUEStage3OK TimeStage3OK TimeUEStage4OK TimeStage4OK TimeOKFinalStageOK finalchoice tables
drop Group Profit TotalProfit Participate roundprofits paid_period1 paid_period2 paid_period3 paid_period4 paid_period5 payoff1 payoff2
save "S2T0.dta", replace
clear all


ztree2stata subjects using 191129_1312.xls
gen Session = 2
keep if Period > 0
drop TimeOKStage0OK TimeStage1OpenFirstOK TimeStage2MoveOK TimeUEStage3OK TimeStage3OK TimeUEStage4OK TimeStage4OK TimeOKFinalStageOK
finalchoice tables
drop Group Profit TotalProfit Participate roundprofits paid_period1 paid_period2 paid_period3 paid_period4 paid_period5 payoff1 payoff2
save "S2T1.dta", replace
clear all


use "S1T0.dta", clear
append using "S1T1.dta"
append using "S2T0.dta"
append using "S2T1.dta"


** Add labels
label variable openfirst "Safe box first"
label variable pos2      "Potential Content of safe Box"
label variable box1      "Content of Risky box"
label variable box2      "Content of Safe box"   
label variable Subject   "Subjects 1-15"
label variable Period    "Rounds 1-100"   
label variable take1oropen2 "Subject's choices"   
label variable take2oropen1 "Subject's choices" 
label variable treatment     "0=strict ,  1=flexible" 
label variable Payoff   "Subject's Payoff per round"
label variable Session  "Session 1-2"
label variable session "Session date"


** Generate Identifier based on Session, treatment and Subject
egen ID2 = group(session treatment Subject)
label variable ID2 "Identifier based on Session, treatment and Subject"

** Generate interaction term treatment * Potential Content of Box2
gen inter = treatment * pos2
label variable inter "treatment * potential content of safe box"

** openfirst (i.e. safeboxfirst) = 1 if safe box is opened and =0 if risky box is opened.
replace openfirst = openfirst - 1

** Generate number of opened boxes
gen openedboxes = .
replace openedboxes = 1 if openfirst == 0 & take1oropen2 == 1 & treatment ==0
replace openedboxes = 1 if openfirst == 1 & take2oropen1 == 1 & treatment ==0
replace openedboxes = 2 if openfirst == 0 & take1oropen2 == 2 & treatment ==0
replace openedboxes = 2 if openfirst == 1 & take2oropen1 == 2 & treatment ==0
replace openedboxes = 1 if openfirst == 0 & take1oropen2 == 1 & treatment ==1
replace openedboxes = 1 if openfirst == 1 & take2oropen1 == 1 & treatment ==1
replace openedboxes = 1 if openfirst == 0 & take1oropen2 == 10 & treatment ==1
replace openedboxes = 1 if openfirst == 1 & take2oropen1 == 10 & treatment ==1
replace openedboxes = 2 if openfirst == 0 & take1oropen2 == 2 & treatment ==1
replace openedboxes = 2 if openfirst == 1 & take2oropen1 == 2 & treatment ==1
label variable openedboxes "number of opened boxes"

** Success rate based on 1st move
gen correct1_r0 = .
label variable correct1_r0 "Success rate based on 1st move"
replace correct1_r0 = 1 if openfirst == 0 & pos2<60  & treatment == 0
replace correct1_r0 = 1 if openfirst == 1 & pos2>60 & treatment == 0
replace correct1_r0 = 1 if pos2==60 & treatment == 0
replace correct1_r0 = 0 if treatment == 0 & correct1_r0 == .
replace correct1_r0 = 1 if openfirst == 0 & treatment == 1
replace correct1_r0 = 0 if treatment == 1 & correct1_r0 == .

** Success rate based on all moves
gen     correct2_r0 = .
label variable correct2_r0 "Success rate based on all moves"
replace correct2_r0 = 1 if openfirst == 0 & pos2<60  & treatment == 0  & box1==0     &  take1oropen2==2
replace correct2_r0 = 1 if openfirst == 0 & pos2<60  & treatment == 0  & box1==100   &  take1oropen2==1
replace correct2_r0 = 1 if openfirst == 1 & pos2>60  & treatment == 0  & box2==0     &  take2oropen1==2 
replace correct2_r0 = 1 if openfirst == 1 & pos2>60  & treatment == 0  & box2==pos2  &  take2oropen1==1
replace correct2_r0 = 1 if openfirst == 1 & pos2>60  & treatment == 0  & box2==pos2  &  take2oropen1==1


replace correct2_r0 = 1 if openfirst == 1 & pos2==60 & treatment == 0  & box2==pos2  &  take2oropen1==1
replace correct2_r0 = 1 if openfirst == 1 & pos2==60 & treatment == 0  & box2==0     &  take2oropen1==2

replace correct2_r0 = 1 if openfirst == 0 & pos2==60 & treatment == 0  & box1==100   &  take1oropen2==1
replace correct2_r0 = 1 if openfirst == 0 & pos2==60 & treatment == 0  & box1==0     &  take1oropen2==2

replace correct2_r0 = 0 if openfirst == 1 & pos2==60 & treatment == 0  & box2==0     & take2oropen1==1
replace correct2_r0 = 0 if openfirst == 1 & pos2==60 & treatment == 0  & box2==pos2  & take2oropen1==2
replace correct2_r0 = 0 if openfirst == 0 & pos2==60 & treatment == 0  & box1==100   & take1oropen2==2
replace correct2_r0 = 0 if openfirst == 0 & pos2==60 & treatment == 0  & box1==0     & take1oropen2==1

replace correct2_r0 = 0 if correct2_r0 != 1   &  treatment ==0 
replace correct2_r0 = 1 if openfirst == 0  &  treatment ==1  & box1==0    &  take1oropen2==10
replace correct2_r0 = 1 if openfirst == 0  &  treatment ==1  & box1==100  &  take1oropen2==1
replace correct2_r0 = 0 if correct2_r0 !=1 &  treatment ==1
replace correct2_r0 = 0 if correct2 != 1   &  treatment ==1
order ID2 Session session treatment Subject openfirst box1 box2 pos2 Payoff inter openedboxes correct1_r0 correct2_r0
save Main.dta, replace


***************************
***** end of do file ******
***************************

